package cc.magicjson.minigame.demo.ddd.domain.service;

import cc.magicjson.minigame.demo.ddd.domain.event.DomainEventPublisher;
import cc.magicjson.minigame.demo.ddd.domain.event.GameCreatedEvent;
import cc.magicjson.minigame.demo.ddd.domain.event.GameStartedEvent;
import cc.magicjson.minigame.demo.ddd.domain.model.Game;
import cc.magicjson.minigame.demo.ddd.domain.model.GameId;
import cc.magicjson.minigame.demo.ddd.domain.model.GameName;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class GameService {
    private final DomainEventPublisher eventPublisher;

    public GameService(@Qualifier("antiCorruptionDomainEventPublisher") DomainEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public Game createGame(GameId id, GameName name) {
        Game game = new Game(id, name);
        eventPublisher.publish(new GameCreatedEvent(game));
        return game;
    }

    public void startGame(Game game) {
        game.start();
        eventPublisher.publish(new GameStartedEvent(game));
    }
}